package com.example.sekerim;

import java.util.ArrayList;
import java.util.Calendar;

import com.example.sekerim.IntroPageActivity.UserLoginTask;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioButton;


public class RegistrationActivityAccountInformation extends Activity {

	private TextView mWelcomeTextView;
	private EditText mUserName;
	private EditText mUserSurname;
	private EditText mMailAddress;
	private EditText mPassword;
	private EditText mPasswordConfirmation;
	private Button mPickDate;
	private RadioButton mRadioMaleButton;
	private RadioButton mRadioFemaleButton;
	private Button mRegistrationNextButton;

	private int mYear;
	private int mMonth;
	private int mDay;

	static final int DATE_DIALOG_ID = 0;
	private boolean isMale = true;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registration_account_information);

		initializePageElements();

		mPickDate = (Button) findViewById(R.id.registration_date_picker_button);
		mPickDate.setText(Constants.registration_birthday_button_text);
		mPickDate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});

		// get the current date
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		// display the current date

		mRadioMaleButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				isMale = true;

			}
		});
		mRadioFemaleButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				isMale = false;

			}
		});

		mRegistrationNextButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				validateForm();
			}
		});

	}



	private DatePickerDialog.OnDateSetListener mDateSetListener =
			new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, 
				int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this,
					mDateSetListener,
					mYear, mMonth, mDay);
		}
		return null;
	}

	public void initializePageElements()
	{
		mWelcomeTextView = (TextView) findViewById(R.id.registration_account_info_page_title);
		mUserName = (EditText) findViewById(R.id.registration_user_name_field);
		mUserSurname = (EditText) findViewById(R.id.registration_user_surname_field);
		mMailAddress = (EditText) findViewById(R.id.registration_mail_address_field);
		mPassword = (EditText) findViewById(R.id.registration_pasword_field);
		mPasswordConfirmation = (EditText) findViewById(R.id.registration_pasword_confirmation_field);
		mRadioMaleButton = (RadioButton) findViewById(R.id.registration_radio_male);
		mRadioFemaleButton = (RadioButton) findViewById(R.id.registration_radio_female);
		mRegistrationNextButton = (Button) findViewById(R.id.registration_next_button);


		mWelcomeTextView.setText(Constants.registration_account_info_page_title);
		mUserName.setHint(Constants.registration_user_name_field);
		mUserSurname.setHint(Constants.registration_user_surname_field);
		mMailAddress.setHint(Constants.registration_mail_address);
		mPassword.setHint(Constants.registration_passwod);
		mPasswordConfirmation.setHint(Constants.registration_pasword_confirmation);
		mRadioMaleButton.setText(Constants.registration_male_button);
		mRadioFemaleButton.setText(Constants.registration_female_button);
		mRegistrationNextButton.setText(Constants.registration_next_button);

		Typeface font = Typeface.createFromAsset(getAssets(), "fonts/beryliumbold.ttf");
		mWelcomeTextView.setTypeface(font);
	}

	public void validateForm()
	{

		mUserName.setError(null);
		mUserSurname.setError(null);
		mMailAddress.setError(null);
		mPassword.setError(null);
		mPasswordConfirmation.setError(null);

		// Store values at the time of the login attempt.
		String userName = mUserName.getText().toString();
		String userSurname = mUserSurname.getText().toString();
		String mailAddress = mMailAddress.getText().toString();
		String password = mPassword.getText().toString();
		String password_confirmation = mPasswordConfirmation.getText().toString();

		boolean cancel = false;
		View focusView = null;


		if (!password.contentEquals(password_confirmation)) {
			mPasswordConfirmation.setError(Constants.registration_no_password_match_error);
			focusView = mPasswordConfirmation;
			cancel = true;
		}
		if (TextUtils.isEmpty(password_confirmation)) {
			mPasswordConfirmation.setError(Constants.registration_empty_password_confirmation_field_error);
			focusView = mPasswordConfirmation;
			cancel = true;
		}
		if (TextUtils.isEmpty(password)) {
			mPassword.setError(Constants.registration_empty_password_field_error);
			focusView = mPassword;
			cancel = true;
		}
		if (TextUtils.isEmpty(mailAddress)) {
			mMailAddress.setError(Constants.registration_empty_mail_address_field_error);
			focusView = mMailAddress;
			cancel = true;
		}
		if (TextUtils.isEmpty(userSurname)) {
			mUserSurname.setError(Constants.registration_empty_user_surname_field_error);
			focusView = mUserSurname;
			cancel = true;
		}
		if (TextUtils.isEmpty(userName)) {
			mUserName.setError(Constants.registration_empty_user_name_field_error);
			focusView = mUserName;
			cancel = true;
		}

		if (cancel) {
			focusView.requestFocus();
		} else {
			Intent intent = new Intent(RegistrationActivityAccountInformation.this,RegistrationActivityPersonalInformation.class); 
			Bundle extras = new Bundle();
			extras.putString("userName", userName);
			extras.putString("userSurname", userSurname);
			extras.putString("mailAddress", mailAddress);
			extras.putString("password", password);
			extras.putBoolean("isMale", isMale);
			extras.putInt("mDay",mDay);
			extras.putInt("mMonth",mMonth+1);
			extras.putInt("mYear",mYear);

			intent.putExtras(extras);
			startActivity(intent);
			
		}

	}
}
